////////academic freedom project/////
clear 
cd C:\個人學術著作\Frank_temp\Data\cleandata
use academicfreedom_masterfile_foranalysis_clean.dta
sort country_name year
merge country_name year using Robustness_KEY_Independent_Variables_2022_7_3.dta 
keep if flag_1981==1
keep if inrange(year, 1981, 2007)
drop if isocode=="DEU"
unique isocode
/// 17
drop _merge
sort isocode year
merge isocode year using OECD_population.dta
keep if _merge==3
egen isocode_fakid=group(isocode)

/// 處理遺失值 ///
//CHE (前九年)
recode v2cacritic(.=1.937)
//AUS (1999)
recode worldtrade(.=1093)
//CHE (1987)
recode c_ppp_nomissing(.=764.93)

gen ln_all_2=ln( all_publication_2 )
gen ln_pub1_2=ln( publication1_2 )
gen ln_pub2_2=ln( publication2_2 )
gen ln_pub3_2=ln( publication3_2 )
gen ln_pub4_2=ln( publication4_2 )
gen ln_pub5_2=ln( publication5_2 )
gen ln_RD=ln(c_ppp_nomissing)
gen ln_population=ln(OECD_population)

save projectdata.dta, replace 

use projectdata.dta, clear 
global X1 univpc sciorgct gdp_pc_curr_US_dollars_Olga worldtrade polity2 ln_RD ln_population

// ssc install asdoc, replace
preserve 
keep if year==1981
asdoc sum $X1 v2clacfree v2cafres v2cacritic v2cafexch ln_all_2 ln_pub1_2 ln_pub2_2 ln_pub3_2 ln_pub4_2 ln_pub5_2
sum2docx $X1 v2clacfree v2cafres v2cacritic v2cafexch ln_all_2 ln_pub1_2 ln_pub2_2 ln_pub3_2 ln_pub4_2 ln_pub5_2 using temp1.docx, replace stats(N mean(%9.2f) sd min(%9.0g) median(%9.0g) max(%9.0g))
restore 

preserve 
keep if year==1993
asdoc sum $X1 v2clacfree v2cafres v2cacritic v2cafexch ln_all_2 ln_pub1_2 ln_pub2_2 ln_pub3_2 ln_pub4_2 ln_pub5_2
sum2docx $X1 v2clacfree v2cafres v2cacritic v2cafexch ln_all_2 ln_pub1_2 ln_pub2_2 ln_pub3_2 ln_pub4_2 ln_pub5_2 using temp2.docx, replace stats(N mean(%9.2f) sd min(%9.0g) median(%9.0g) max(%9.0g))
restore 

preserve 
keep if year==2007
asdoc sum $X1 v2clacfree v2cafres v2cacritic v2cafexch ln_all_2 ln_pub1_2 ln_pub2_2 ln_pub3_2 ln_pub4_2 ln_pub5_2
sum2docx $X1 v2clacfree v2cafres v2cacritic v2cafexch ln_all_2 ln_pub1_2 ln_pub2_2 ln_pub3_2 ln_pub4_2 ln_pub5_2 using temp3.docx, replace stats(N mean(%9.2f) sd min(%9.0g) median(%9.0g) max(%9.0g))
restore 

////final model///
////v2clacfree//// 
xtset isocode_fakid year
est clear 
eststo: xtreg ln_all_2 v2clacfree $X1, fe 
eststo: xtreg ln_pub1_2 v2clacfree $X1, fe 
eststo: xtreg ln_pub2_2 v2clacfree $X1, fe 
eststo: xtreg ln_pub3_2 v2clacfree $X1, fe 
eststo: xtreg ln_pub4_2 v2clacfree $X1, fe 
eststo: xtreg ln_pub5_2 v2clacfree $X1, fe 
esttab using table1.rtf, replace 

////v2cafres/////
xtset isocode_fakid year
est clear 
eststo: xtreg ln_all_2 v2cafres $X1, fe 
eststo: xtreg ln_pub1_2 v2cafres $X1, fe 
eststo: xtreg ln_pub2_2 v2cafres $X1, fe 
eststo: xtreg ln_pub3_2 v2cafres $X1, fe 
eststo: xtreg ln_pub4_2 v2cafres $X1, fe 
eststo: xtreg ln_pub5_2 v2cafres $X1, fe 
esttab using table2.rtf, replace 

////v2cacritic////
xtset isocode_fakid year
est clear 
eststo: xtreg ln_all_2 v2cacritic $X1, fe 
eststo: xtreg ln_pub1_2 v2cacritic $X1, fe 
eststo: xtreg ln_pub2_2 v2cacritic $X1, fe 
eststo: xtreg ln_pub3_2 v2cacritic $X1, fe 
eststo: xtreg ln_pub4_2 v2cacritic $X1, fe 
eststo: xtreg ln_pub5_2 v2cacritic $X1, fe 
esttab using table3.rtf, replace 

////v2cafexch/////
xtset isocode_fakid year
est clear 
eststo: xtreg ln_all_2 v2cafexch $X1, fe 
eststo: xtreg ln_pub1_2 v2cafexch $X1, fe 
eststo: xtreg ln_pub2_2 v2cafexch $X1, fe 
eststo: xtreg ln_pub3_2 v2cafexch $X1, fe 
eststo: xtreg ln_pub4_2 v2cafexch $X1, fe 
eststo: xtreg ln_pub5_2 v2cafexch $X1, fe 
esttab using table4.rtf, replace 

/////scatter plot
preserve 
keep if year==2007
scatter ln_all_2 v2clacfree, mlabel( isocode )
graph save "Graph" "C:\個人學術著作\Frank_temp\Data\Graph_all.gph", replace 
scatter ln_pub1_2 v2clacfree, mlabel( isocode )
graph save "Graph" "C:\個人學術著作\Frank_temp\Data\Graph_q1.gph"
scatter ln_pub2_2 v2clacfree, mlabel( isocode )
graph save "Graph" "C:\個人學術著作\Frank_temp\Data\Graph_q2.gph"
scatter ln_pub3_2 v2clacfree, mlabel( isocode )
graph save "Graph" "C:\個人學術著作\Frank_temp\Data\Graph_q3.gph"
scatter ln_pub4_2 v2clacfree, mlabel( isocode )
graph save "Graph" "C:\個人學術著作\Frank_temp\Data\Graph_q4.gph"
scatter ln_pub5_2 v2clacfree, mlabel( isocode )
graph save "Graph" "C:\個人學術著作\Frank_temp\Data\Graph_q5.gph"

cd C:\個人學術著作\Frank_temp\Data\
graph combine Graph_all.gph Graph_q1.gph Graph_q2.gph Graph_q3.gph Graph_q4.gph Graph_q5.gph
restore